package com.thunder.soft.ui.activity;

import android.widget.Toast;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.thunder.soft.R;
import com.thunder.soft.bean.OpenSourceBean;
import com.thunder.soft.common.base.BaseActivity;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.ActivityOpenSourceBinding;
import com.thunder.soft.ui.adapter.OpenSourceAdapter;
import com.thunder.soft.vm.OpenSourceViewModel;

import java.util.List;

public class OpenSourceActivity extends BaseActivity<ActivityOpenSourceBinding> {

    private OpenSourceViewModel openSourceViewModel;

    @Override
    protected void initData() {

        openSourceViewModel = ViewModelProviderHelper.buildViewModel(this, OpenSourceViewModel.class);
        getLifecycle().addObserver(openSourceViewModel);
        binding.setOpenSourceViewModel(openSourceViewModel);

        openSourceViewModel.getErrorMsg().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(OpenSourceActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        });

        // 关闭页面
        openSourceViewModel.getFinishActivity().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                finish();
            }
        });

        OpenSourceAdapter openSourceAdapter = new OpenSourceAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        binding.recyclerView.setAdapter(openSourceAdapter);

        openSourceViewModel.getLiveData().observe(this, new Observer<List<OpenSourceBean>>() {
            @Override
            public void onChanged(List<OpenSourceBean> openSourceBeans) {
                openSourceAdapter.addList(openSourceBeans);
            }
        });
        openSourceViewModel.listData();
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_open_source;
    }
}